package com.spsoft.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spsoft.core.enums.EnableStatusEnum;
import com.spsoft.core.model.Result;
import com.spsoft.core.utils.BeanUtils;
import com.spsoft.core.utils.SecurityUtils;
import com.spsoft.system.entity.Framedepartment;
import com.spsoft.system.model.vo.FramedepartmentListVO;
import com.spsoft.system.model.vo.OrganVo;
import com.spsoft.system.service.FramedepartmentService;
import com.spsoft.system.mapper.FramedepartmentMapper;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 部门表
 *
 * @author wangy
 */
@Service
public class FramedepartmentServiceImpl extends ServiceImpl<FramedepartmentMapper, Framedepartment>
        implements FramedepartmentService {

    @Override
    public Result<List<FramedepartmentListVO>> getDeptList() {
        List<FramedepartmentListVO> collect = list(new LambdaQueryWrapper<Framedepartment>()
                .eq(Framedepartment::getDeptStatus, "1"))
                .stream().map(item -> {
                    FramedepartmentListVO vo = new FramedepartmentListVO();
                    BeanUtils.copyBeanProp(vo, item);
                    return vo;
                }).collect(Collectors.toList());
        return Result.OK(collect);
    }

    @Override
    public Result<OrganVo> getDeptList(boolean isAll) {
        String deptCode= SecurityUtils.getDeptCode();
        List<Framedepartment> list = list(new LambdaQueryWrapper<Framedepartment>()
                .eq(Framedepartment::getDeptStatus, EnableStatusEnum.Enable.getCode())
                .like(!isAll, Framedepartment::getReserve10, deptCode));
        Map<String,List<OrganVo>> dataMap = new HashMap<>();  // 用于按照fid分组保存数据
        OrganVo organVo= null;
        for(Framedepartment t:list){
            OrganVo deptOrganVo=new OrganVo();
            BeanUtils.copyBeanProp(deptOrganVo,t);

            if(isAll && "TOP".equals(t.getParaCode())){
                organVo = deptOrganVo;
            }else if(!isAll && deptCode.equals(t.getDeptCode())){
                organVo = deptOrganVo;
            }
            if(dataMap.containsKey(t.getParaCode())){
                dataMap.get(t.getParaCode()).add(deptOrganVo);
            }else{
                List<OrganVo> voList = new ArrayList<>();
                voList.add(deptOrganVo);
                dataMap.put(t.getParaCode(),voList);
            }
        }
        List<OrganVo> listOrganVo= getSubDept(dataMap, Arrays.asList(organVo));
        return Result.OK(listOrganVo.get(0));
    }

    /**
     * 递归获取子部门
     * 根据dataMap 在根据传送的上级，获取下级部门
     */
    public List<OrganVo> getSubDept(Map<String,List<OrganVo>> dataMap, List<OrganVo> dataList){
        if(dataList==null || dataList.size()==0){
            return new ArrayList<>();
        }
        List<OrganVo> retList = new ArrayList<>();
        for(OrganVo vo:dataList){
            OrganVo orVo = new OrganVo();
            BeanUtils.copyBeanProp(orVo,vo);
            if(dataMap.containsKey(vo.getDeptCode())){
                List<OrganVo> organVos = dataMap.get(vo.getDeptCode());
                dataMap.remove(vo.getDeptCode());
                List<OrganVo> subDepts=getSubDept(dataMap,organVos);
                orVo.setChildren(subDepts);
            }else{
                orVo.setChildren(new ArrayList<>());
            }
            retList.add(orVo);
        }
        return retList;
    }
}




